#ifndef DmxSetter_H
#define DmxSetter_H

#include <QObject>
#include <QTime>

#include "DmxValueList.h"

class SwitchWatcher;
class DmxDevice;

class DmxSetter : public QObject
{
	Q_OBJECT
public:
	DmxSetter(QObject *parent=0);
	~DmxSetter();
	
	SwitchWatcher * switchWatcher() { return m_switchWatcher; }
	bool isSwitchMaster() { return m_isSwitchMaster; }
	
	DmxValueList switchOnProfile() { return m_switchOnProfile; }
	
public slots:
	// This should be called by the GUI module
	void setSwitchMaster(bool);
	
	// Transmit a single DMX value.
	// Really, just calls setDmxChannels() with the appros array index updated
	void setDmxChannel(int chan, int val); // chan 0-512, val 0-255
	
	// Set all channels in the current DMX universe in one go
	// TODO: How do we change the DMX universe?
	// NOTE: If isSwitchMaster() is TRUE, then this has NO EFFECT
	void setDmxChannels(DmxValueList);
	
	// Store a new "switch on" profile (default is all channels=255)
	void setSwitchOnProfile(DmxValueList);
	
private slots:
	// Receive the toggle signal from the switch watcher
	void switchToggled(bool);
	
// 	// Internally actually does the transmitting - actually, just calls the next setDmx()
// 	// with the appros array index updated 
// 	void sendDmx(int chan, int val);
	
	// Sets m_channelStates and sends it out to the connected ENTTEC PRO widget
	void sendDmx(DmxValueList);
	
private: /* methods */
	bool initDmx();
	void closeDmx();

private:
	bool m_isSwitchOn;
	bool m_isSwitchMaster;
	
	//our DMX packet (which holds the channel values + 1st byte for the start code)
	DmxValueList m_channelStates;
	
	// What to set when setSwitchMaster(false) is called - this is set with current value of m_channelStates when setSwitchMaster(true) is called 
	DmxValueList m_channelStates_beforeSwitchMaster;
	
	// 'switch on' profile - the DMX values to use when the the switch is master (m_isSwitchMaster) and the switch is toggled on
	DmxValueList m_switchOnProfile;
	
	// An alternate 'switch on' profile - triggered if m_enableAltProfile AND if the next switchToggled(true) is received within 'm_altSwitchTriggerTime' ms 
	DmxValueList m_switchOnAltProfile;
	
	// flag to enable the alt profile functionality
	bool m_enableAltProfile;
	
	// The max length of time between switchToggled(true) signals which will trigger the alt profile 
	int m_altSwitchTriggerTime;
	
	// Timer of last switch on - used to trigger the alt profile
	QTime m_lastSwitchOnTime;
	
	// DMX output interface
	DmxDevice *m_dmxInterface;
	
	// Watch the switch for override
	SwitchWatcher *m_switchWatcher;
};

#endif
